package com.ruoyi.common.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Random;

/**
 * <p>
 * 用户积分变动记录表，记录用户积分的所有变动情况
 * </p>
 *
 * @author jiabao
 * @since 2025-06-25
 */
@Getter
@Setter
@ToString
@Builder
@AllArgsConstructor
@NoArgsConstructor
@TableName("business_point_records")
@ApiModel(value = "PointRecords对象", description = "用户积分变动记录表，记录用户积分的所有变动情况")
public class PointRecords implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 记录ID，主键自增
     */
    @ApiModelProperty("记录ID，主键自增")
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 关联business_users表的用户ID
     */
    @TableField("user_id")
    @ApiModelProperty("关联business_users表的用户ID")
    private Long userId;

    /**
     * 积分变动值(正数为增加，负数为减少)
     */
    @TableField("points")
    @ApiModelProperty("积分变动值(正数为增加，负数为减少)")
    private Integer points;

    /**
     * 变动后的积分余额(记录当前积分)
     */
    @TableField("balance")
    @ApiModelProperty("变动后的积分余额(记录当前积分)")
    private Integer balance;

    /**
     * 类型：1-评论奖励 2-消费扣除 3-提现扣除 4-管理员调整
     */
    @TableField("point_type")
    @ApiModelProperty("类型：1-评论奖励 2-消费扣除 3-提现扣除 4-管理员调整")
    private String pointType;

    @Getter
    public enum POINT_TYPE {
        COMMENT_REWARD("评论奖励", "COMMENT_REWARD"),
        CONSUMPTION_DEDUCTION("消费扣除", "CONSUMPTION_DEDUCTION"),
        WITHDRAWAL_DEDUCTION("提现扣除", "WITHDRAWAL_DEDUCTION"),
        ADMIN_ADJUSTMENTS("管理员调整", "ADMIN_ADJUSTMENTS")

        ;


        private final String desc;
        private final String value;

        POINT_TYPE(String desc, String value) {
            this.desc = desc;
            this.value = value;
        }

        // 返回随机枚举值的value字符串
        public static String getRandom() {
            POINT_TYPE[] values = values();
            return values[new Random().nextInt(values.length)].value;
        }

        // 返回随机枚举实例
        public static POINT_TYPE random() {
            POINT_TYPE[] values = values();
            return values[new Random().nextInt(values.length)];
        }

        // 通过value获取枚举实例
        public static POINT_TYPE fromValue(String value) {
            for (POINT_TYPE type : values()) {
                if (type.value.equals(value)) {
                    return type;
                }
            }
            throw new IllegalArgumentException("未知的状态类型: " + value);
        }
    }


    /**
     * 关联业务ID(订单ID/评论ID等，记录积分来源)
     */
    @TableField("related_id")
    @ApiModelProperty("关联业务ID(订单ID/评论ID等，记录积分来源)")
    private Long relatedId;

    /**
     * 变动描述(记录积分变动原因)
     */
    @TableField("description")
    @ApiModelProperty("变动描述(记录积分变动原因)")
    private String description;

    /**
     * 创建时间
     */
    @ApiModelProperty("创建时间")
    @TableField("created_at")
    private LocalDateTime createdAt;
}
